<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if(!class_exists('Tmp_api_survey')){
    class Tmp_api_survey extends CI_Controller {

        protected $_action = '';

        private $_source = '';
        private $_type = '';

        public function __construct(){
            parent::__construct();
            $this->load->model('Apilogmodel','m_api_log');
            $this->load->model('Usermodel','m_user');
            $this->load->model('Appmodel','m_app');
            $this->load->model('Smsmodel','m_sms');
            $this->load->model('Hdsurveymodel','m_survey');
            $this->load->database();
        }

        public function updateCustomerNew() {
            $query = $this->db->query('select * from mpc_hd_survey_customer where customer_id>=1352 and customer_id<2091');
            $customer = $query->result_array();

            $customer_ids = extractColumn($customer,'customer_id');

            $query = $this->db->query('select * from mpc_api_log where request_uri like "%api_hd_survey/initCustomer%" and customer_id in ('
                .implode(',',$customer_ids).') and result_data like \'%{"bind":true%\'');
            $list = $query->result_array();
            $list = reindexArray($list,'customer_id');
            echo json_encode(array_keys($list));
            die();
            $arr = array();
            foreach ($customer as $key => $value) {
                if(isset($list[$value['customer_id']])) {
                    $post_data = json_decode($list[$value['customer_id']]['post_data'],true);
                    $arr[] = array(
                        'customer_id' => $value['customer_id'],
                        'openid' => $post_data['openid'],
                        'create_code' => $post_data['create_code'],
                    );
                }
            }
            $this->db->update_batch('hd_survey_customer',$arr,'customer_id');
            echo count($arr);

        }


        public function initCustomer() {
            $query = $this->db->query('select * from mpc_api_log where request_uri like "%api_hd_survey/bind%"');
            $list = $query->result_array();
            $arr = array();
            foreach ($list as $key => $value) {
                $result_data = json_decode($value['result_data'],true);
                if($result_data['success'] == false) continue;
                $post_data = json_decode($value['post_data'],true);
                $arr[] = array(
                    'customer_id' => 2091 + $value['customer_id'],
                    'ip' => $value['ip_address'],
                    'customer_time_create' => $value['api_log_time_create'],
                    'customer_phone' => $post_data['customer_phone'],
                    'customer_car_number' => $post_data['customer_car_number'],
                );
            }
            echo json_encode($arr);
            $this->db->insert_batch('hd_survey_customer',$arr);
            echo 'success';
        }

        public function initSurvey() {
            $query = $this->db->query('select * from mpc_api_log where request_uri like "%api_hd_survey/submitSurvey%"');
            $list = $query->result_array();
            $arr = array();
            $existd = array();
            $customer_arr = array();
            foreach ($list as $key => $value) {
                $result_data = json_decode($value['result_data'],true);
                if($result_data['success'] == false) continue;
                $post_data = json_decode($value['post_data'],true);
                if($value['customer_id'] == 0 || isset($existd[$value['customer_id']])) continue;
                $existd[$value['customer_id']] = 1;
                $post_data['customer_id'] = 2091 + $value['customer_id'];
                $post_data['customer_time_create'] = $value['api_log_time_create'];
                $arr[] = $post_data;
            }
            // echo json_encode($arr);
            // die();
            $this->db->insert_batch('hd_survey_content',$arr);
            // $this->db->update_batch('hd_survey_customer',$customer_arr,'customer_id');
            echo 'success';
        }

        public function deleteCustomer() {
            @set_time_limit(0);
            $query = $this->db->query('select * from mpc_hd_survey_customer');
            $list = $query->result_array();
            $existed = [];
            $deleteId = [];
            foreach ($list as $key => $value) {
                if(!isset($existed[$value['openid']])) {
                    $existed[$value['openid']] = $value;
                } else {
                    if($value['content_id'] > 0) {
                        $existed[$value['openid']] = $value;
                    } else {
                        $deleteId[] = $value['customer_id'];
                    }
                }
            }
            $this->db->where_in('customer_id',$deleteId);
            $this->db->delete('hd_survey_customer');
            echo json_encode($deleteId);
        }

        public function updateCustomer() {
            @set_time_limit(0);

            $query = $this->db->query('select * from mpc_hd_survey_customer where openid="" order by customer_id asc');
            $list = $query->result_array();
            // $arr = array();
            $existd = array();
            // $customer_arr = array();

            foreach ($list as $key => $value) {
                // $result_data = json_decode($value['result_data'],true);
                // if($result_data['success'] == false) continue;
                // $post_data = json_decode($value['post_data'],true);
                // if(empty($post_data['create_code'])) continue;
                // $customer_id = 0;
                $query = $this->db->query('select * from mpc_api_log where request_uri like "%api_hd_survey/initCustomer%" and ip_address="'
                    .$value['ip'].'" and api_log_time_create<"'.$value['customer_time_create'].'" and api_log_time_create>"'
                    .date('Y-m-d H:i:s',strtotime($value['customer_time_create'])-20*60).'" and result_data like \'%"success":true,%\' limit 1');
                $info = $query->row_array();
                echo json_encode($info);
                if(empty($info)) continue;
                $result_data = json_decode($info['result_data'],true);
                if($result_data['success'] == false) continue;

                $post_data = json_decode($info['post_data'],true);

                    // $existd[$post_data['openid']] = 1;
                    $arr = array(
                        'customer_id' => $value['customer_id'],
                        'openid' => $post_data['openid'],
                        'create_code' => $post_data['create_code'],
                    );
                    $this->db->where('customer_id',$arr['customer_id']);
                    $this->db->update('hd_survey_customer',$arr);


                // echo json_encode($info);
                // echo $key.'<br/>';
                // break;
            }
            // die();
            // echo json_encode($arr);
            // die();
            // $this->db->insert_batch('hd_survey_content',$arr);
            // $this->db->update_batch('hd_survey_customer',$customer_arr,'customer_id');
            echo 'success';

        //     $query = $this->db->query('select * from mpc_api_log where request_uri like "%api_hd_survey/initCustomer%"');
        //     $list = $query->result_array();
        //     // $arr = array();
        //     $existd = array();
        //     // $customer_arr = array();
        //
        //     foreach ($list as $key => $value) {
        //         $result_data = json_decode($value['result_data'],true);
        //         if($result_data['success'] == false) continue;
        //         $post_data = json_decode($value['post_data'],true);
        //         if(empty($post_data['create_code'])) continue;
        //         $customer_id = 0;
        //
        //         $query = $this->db->query('select * from mpc_hd_survey_customer where openid="" and ip="'.$value['ip_address'].'" order by customer_id asc limit 1');
        //         $info = $query->row_array();
        //         if(empty($info)) continue;
        //         $second = strtotime($value['api_log_time_create']) - strtotime($info['customer_time_create']);
        //         if($second < 5*60 && $second > -5*60) {
        //             // $existd[$post_data['openid']] = 1;
        //             $arr = array(
        //                 'customer_id' => $info['customer_id'],
        //                 'openid' => $post_data['openid'],
        //                 'create_code' => $post_data['create_code'],
        //             );
        //             $this->db->where('customer_id',$arr['customer_id']);
        //             $this->db->update('hd_survey_customer',$arr);
        //         }
        //
        //         // echo json_encode($info);
        //         // echo $key.'<br/>';
        //         // break;
        //     }
        //     // die();
        //     // echo json_encode($arr);
        //     // die();
        //     // $this->db->insert_batch('hd_survey_content',$arr);
        //     // $this->db->update_batch('hd_survey_customer',$customer_arr,'customer_id');
        //     echo 'success';
        }
    }
}
